//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.Provenance.Controls.View
{
    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    using Microsoft.Research.Provenance.Controls.Presenter;
    using Microsoft.Research.ScientificWorkflow.TridentModel;
    using Microsoft.Research.ScientificWorkflow.UIDesigner;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;

    /// <summary>
    /// Interaction logic for DetailProvenanceInformationView.xaml
    /// </summary>
    public partial class DetailProvenanceInformationView : UserControl
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        public DetailProvenanceInformationView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the DetailedProvenancePresenter Model to the view's data context.
        /// </summary>
        public DetailedProvenancePresenter Presenter
        {
            get { return this.DataContext as DetailedProvenancePresenter; }
            set { this.DataContext = value; }
        }

        /// <summary>
        /// Initialize composer control.
        /// </summary>
        /// <param name="workflowModel">The workflow model</param>
        public void Initialize(TridentWorkflowModel workflowModel, Dictionary<string, DataTypeModel> dataTypes)
        {
            // Add the composer control.
            ComposerControl composerControl = new ComposerControl(null, null, dataTypes, RenderMode.ProvenanceMode);
            composerControl.Unloaded += new RoutedEventHandler(composerControl_Unloaded);
            composerControl.Loaded += new RoutedEventHandler(composerControl_Loaded);
            composerControl.Name = "provenanceComposer";
            composerControl.ProvenanceMenuClick += new EventHandler<ProvenanceModeEventArgs>(ComposerControl_ProvenanceMenuClick);

            // Make the provenance mode true.
            workflowModel.IsProvenanceMode = true;
            
            workflowModel.SwapSubsectionState += delegate(object sender, EventArgs e) { composerControl.SwapSubsectionState(sender); };

            this.composerHost.Child = composerControl;

            composerControl.LoadWorkflow(workflowModel, false);
        }

        /// <summary>
        /// Adds views to the details host.
        /// </summary>
        /// <param name="control"></param>
        public void AddViews(UIElement control)
        {
            this.activityDetailHost.Children.Add(control);
        }

        /// <summary>
        /// Event handler for provenance context menu click event.
        /// Raise respective commands.
        /// </summary>
        /// <param name="sender">Activity base model.</param>
        /// <param name="e">ProvenanceModeEventArgs.</param>
        private void ComposerControl_ProvenanceMenuClick(object sender, ProvenanceModeEventArgs e)
        {
            ProvenanceMenu selectedOption = e.OptionSelected;

            switch (selectedOption)
            {
                case ProvenanceMenu.DataProducts:
                    Presenter.DisplayDataProducts.Execute(e.ActivityName);
                    break;
                case ProvenanceMenu.InputOutput:
                    Presenter.DisplayInputOutput.Execute(e.ActivityName);
                    break;
                case ProvenanceMenu.Performance:
                    Presenter.DisplayPerformance.Execute(e.ActivityName);
                    break;
                case ProvenanceMenu.ProcessingStatus:
                    Presenter.DisplayProcessingStatus.Execute(e.ActivityName);
                    break;
            }
        }

        /// <summary>
        /// The event handler for the loaded event of the composer control.
        /// Register the name of the control in the main window.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The event arguments</param>
        private void composerControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                ComposerControl control = sender as ComposerControl;
                object someControl = Application.Current.MainWindow.FindName(control.Name);
                if (someControl != null)
                {
                    Application.Current.MainWindow.UnregisterName(control.Name);
                }

                Application.Current.MainWindow.RegisterName(control.Name, control);
            }
            catch (NullReferenceException exception)
            {
                TridentErrorHandler.HandleUIException(exception);
                TridentMessageBox.ShowTridentErrorMessageBox("Unable to load detailed provenance ");
            }
        }

        /// <summary>
        /// The event handler for the unloaded event of the composer control.
        /// Unregister the name of the control from the main window.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The event arguments</param>
        private void composerControl_Unloaded(object sender, RoutedEventArgs e)
        {
            ComposerControl control = sender as ComposerControl;
            object someControl = Application.Current.MainWindow.FindName(control.Name);
            if (someControl != null)
            {
                Application.Current.MainWindow.UnregisterName(control.Name);
            }
        }

        /// <summary>
        /// The event handler for expanded control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">RoutedEventArgs.</param>
        private void OnExpanded(object sender, RoutedEventArgs e)
        {
            if (!this.Presenter.IsUpdated)
            {
                this.Presenter.GetProvenanceDetail.Execute(null);
            }
        }
    }
}
